<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class JuheDate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:JuheDate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->getWeather();
        $this->getAirQuality();
        return 0;
    }

    /**
     * @return void getWeather
     */
    public function getWeather()
    {
        try {
            $response = Http::asForm()->get(env('JUHE_WEATHER_URL'), ['key' => env('JUHE_WEATHER_KEY'), 'city' => '三亚']);
            if ($response->ok()) {
                Cache::put(env('JUHE_WEATHET_CACHE_KEY'), $response->json(), $seconds = 24 * 3600);
                //Log::info($response->json());
            }
        } catch (\Exception $e) {
            Log::error($e->getMessage());
        }
    }

    /**
     * @return void getWeather
     */
    public function getAirQuality()
    {
        try {
            // 根据集合数据接口，三亚市坐标信息为Id": "225","CityName": "三亚市","CityCode": "460200","CityJC": "SYS"， 这里干脆写死
//            $val = $request->validate([
//                'city' => 'required',
//            ]);
            $response = Http::asForm()->get(env('JUHE_AIR_QUALITY_URL'), ['key' => env('JUHE_AIR_QUALITY'), 'cityId' => 225]);
            if ($response->ok()) {
                Cache::put(env('JUHE_AIR_CACHE_KEY'), $response->json(), $seconds = 24 * 3600);
                //Log::info($response->json());
            }
        } catch (\Exception $e) {
            Log::error($e->getMessage());
        }
    }

}
